// miniprogram/pages/user/user.js
const db=wx.cloud.database()
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    // integral:0.00,
    Recycle:[],
    avatarUrl:""

  },
  // 获取用户openid
 getOpenid() {
  let that = this;
  wx.cloud.callFunction({
   name: 'getOpenid',
   complete: res => {
    // console.log('云函数获取到的openid: ', res.result.openid)
    app.globalData.openid=res.result.openid
    // console.log('全局openid：'+app.globalData.openid)
    var openid = res.result.openid;
    that.setData({
     openid: openid
    })
   }
  })
 },
 
//  计算积分

  get_money_sum(){
    var integral=0
    let that=this
    let integral_sum=0
    for(var x=0;x<that.data.Recycle.length;x++){
      integral_sum=integral_sum+that.data.Recycle[x].integral
    }
    that.setData({
      integral:integral_sum
    })
    // console.log('integral值为：'+integral)
    // console.log('integral_sum值为：'+integral_sum)
    
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.getOpenid();

     // 获取用户信息
     wx.getSetting({
      success: res => {
        
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              app.globalData.avatarUrl=res.userInfo.avatarUrl;
              console.log('全局avatarUrl'+app.globalData.avatarUrl)
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  

    db.collection('ordermange').get().then(res=>{
      this.setData({
        ordermange:res.data
      })
    }
    )
    db.collection('myservice').get().then(res=>{
      this.setData({
        myservice: res.data
      })
    })
    db.collection('Recycle').get().then(res=>{
      this.setData({
        Recycle: res.data
      })
      this.get_money_sum()
    })

  },

  onGetUserInfo: function(e) {
    console.log('登录获取用户信息'+e.detail.userInfo.avatarUrl)
    var that=this
    
      that.setData({
        // logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo,
        ids:3
      })
      console.log("用户头像"+e.detail.userInfo.avatarUrl)
    
  },
   //app.js
   getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              var userInfo = res.userInfo
              that.globalData.userInfo = res.userInfo;
              typeof cb == "function" && cb(that.globalData.userInfo);
            }
          })
        }
      })
    }
  },

  containerTap: function (res) {
    var k=this
      var x = res.touches[0].pageX;
      var y = res.touches[0].pageY+90 ;
      this.setData({
        rippleStyle: 'top:' + y + 'px;left:' + x + 'px;-webkit-animation: ripple 0.4s linear;animation:ripple 0.4s linear;'
      });
      setTimeout(function() {
        k.setData({
          rippleStyle: ''
        })
      }, 400)
  },

  // 点击九宫格进行跳转
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})